<?php
require_once("../../../util/configuration/configuration_parametters.php");
require_once("../../../util/" . MESSAGES_PROPERTIES . "messages_model.php");
require_once("../../../util/exception/exception_handler.php");

require_once("../../autor_ping/to/autor_ping_to.php");
require_once("../../autor_ping/dao/autor_ping_dao_factory.php");

require_once("../../autor/to/autor_to.php");
require_once("../../autor/dao/autor_dao_factory.php");


class Autor_ping_dao_test {

	private $dao;
	private $autor1;
	private $autor2;
	
	public function __construct() {}
	
	public function before_test() {			
		$this->autor1 = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxx','password','email'));		
		$this->autor2 = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxY','password','email'));		
		
		
		$this->dao = Autor_ping_dao_factory::singleton()->get_dao();
	}
	
	public function run_test() {
		try {
			$date = date("Y-m-d H:i:s");
			$autor_ping = new Autor_ping_to($this->autor1->get_login(), 
													$this->autor2->get_login(), 'comment', 
													'positive', $date, 'open');
			
			$autor_ping = (object) $this->dao->create($autor_ping);		
			echo 'create<br/>';			

			echo 'autor_ping: '; 
			echo $autor_ping->to_string();
			echo '<br/>';
			
			if ($this->dao->exist($autor_ping->get_to_autor_login(), 
									$autor_ping->get_from_autor_login())) {
				
				$result = $this->dao->find_by_to_autor_and_from_autor_login($autor_ping->get_to_autor_login(), 
																		$autor_ping->get_from_autor_login());
				echo 'find<br/>';
				
				$aux_autor_ping = new Autor_ping_to($result['to_autor_login'], $result['from_autor_login'], 
																	$result['comment'],	$result['type'], 
																	$result['date'], $result['state']);
				if ($autor_ping->equals($aux_autor_ping)) {
					$autor_ping->set_state('wait');
					$this->dao->update($autor_ping);
					echo 'update<br/>';
					
					$result = $this->dao->find_by_to_autor_and_from_autor_login($autor_ping->get_to_autor_login(), 
																		$autor_ping->get_from_autor_login());
					$aux_autor_ping = new Autor_ping_to($result['to_autor_login'], $result['from_autor_login'], 
																	$result['comment'],	$result['type'], 
																	$result['date'], $result['state']);
					echo 'autor_ping: ';
					echo $aux_autor_ping->to_string();
					echo '<br/>';		
				}		
					
				$this->dao->remove($autor_ping->get_to_autor_login(), 
									$autor_ping->get_from_autor_login());
				echo 'remove<br/>';
				
			}
		}
		catch (Exception $e) {
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function after_test() {
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor1->get_login());
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor2->get_login());
	}
}

//test
$test = new Autor_ping_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();

?>